<template>
  <div class="analysis-card">
    <h3 class="analysis-title">成交商品TOP5</h3>
    <div class="products-list">
      <div v-for="(item, index) in getData('成交商品TOP5')" :key="index" class="product-item">
        <div class="rank">
          <span class="rank-number">{{ index + 1 }}</span>
        </div>
        <div class="product-image-wrapper">
          <img :src="item.productImage" alt="商品图片" class="product-image">
        </div>
        <div class="product-info">
          <div class="product-header">
            <h4 class="product-name">{{ item.productName }}</h4>
            <span class="sales-amount">¥{{ item.salesAmount?.toLocaleString() }}</span>
          </div>
          <div class="product-footer">
            <div class="package-type">{{ item.packageType }}</div>
            <div class="price-info">
              <span class="current-price">¥{{ item.productPrice?.toFixed(2) }}</span>
              <span class="original-price">¥{{ item.productOriginalPrice?.toFixed(2) }}</span>
            </div>
            <div class="sales-label">成交金额</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import type { ReportSection } from '@/types/report'

interface Props {
  sectionData: ReportSection
}

const props = defineProps<Props>()

const getProductName = (index: number): string => {
  const product = getData(`商品${index}名称`)
  return product === '-' ? '暂无数据' : product
}

const getData = (key: string): any => {
  const item = props.sectionData.items.find((item: { name: string; value: string }) => item.name === key)
  if (!item) return []

  if (key === '成交商品TOP5') {
    try {
      console.log('item',item);
      
      // 1. 替换 HTML 实体为正常字符
      const decodedString = item.value

      // 2. 解析 JSON 字符串为对象
      return decodedString
    } catch (error) {
      console.error('解析商品数据失败:', error)
      return []
    }
  }

  return item?.value || '-'
}
</script>

<style scoped>
.analysis-card {
  margin-bottom: 20px;
  padding: 12px 20px;
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.analysis-title {
  font-size: 18px;
  color: #2d3748;
  font-weight: 600;
  margin-bottom: 10px;
  padding-bottom: 6px;
  border-bottom: 2px solid #e2e8f0;
}

.products-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 8px 0;
}

.product-item {
  display: flex;
  align-items: center;
  padding: 12px;
  background: #ffffff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.rank {
  width: 40px;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16px;
}

.rank-number {
  width: 28px;
  height: 28px;
  background: #4A5568;
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: bold;
}

.product-item:nth-child(1) .rank-number {
  background: #FFB800;
}

.product-item:nth-child(2) .rank-number {
  background: #B4B4B4;
}

.product-item:nth-child(3) .rank-number {
  background: #CD7F32;
}

.product-image-wrapper {
  width: 70px;
  height: 70px;
  margin-right: 16px;
  overflow: hidden;
  border-radius: 8px;
}

.product-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.product-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  min-width: 0;
}

.product-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 8px;
}

.product-name {
  color: #2d3748;
  font-size: 14px;
  font-weight: 500;
  margin: 0;
  padding: 0;
  line-height: 1.3;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.sales-amount {
  font-size: 18px;
  font-weight: bold;
  color: #111827;
  white-space: nowrap;
  margin-left: 8px;
}

.product-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.package-type {
  padding: 2px 6px;
  background: #EDF2F7;
  border-radius: 4px;
  font-size: 12px;
  color: #4A5568;
}

.price-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.current-price {
  font-size: 14px;
  font-weight: 500;
  color: #2D3748;
}

.original-price {
  font-size: 12px;
  color: #A0AEC0;
  text-decoration: line-through;
}

.sales-label {
  font-size: 12px;
  color: #718096;
}
</style>